/* 表单的全局状态 */
import { useState, useCallback, useRef } from 'react';
import getScrollBarSize from '@root/lib/getScrollBarSize';
import useScrollVisible from './useScrollVisible';
import ScrollSyncer from './lib/ScrollSyncer';
const { size: scrollSize, scrollBarType } = getScrollBarSize();

/* state中说明，参考同目录下的tableState.md说明 */
function useTableState() {
    const [tableState, setTableState] = useState({
        scrollSize, scrollBarType,
    });
    const scrollSyncRef = useRef(new ScrollSyncer());
    const { scrollVisible, modifyScrollVisible } = useScrollVisible();
    const modifyState = useCallback((k, v) => {
        setTableState((prevState) => {
            return {
                ...prevState,
                [k]: v
            }
        })
    }, []);
    const scrollRefRegister = useCallback((id, fn) => {
        scrollSyncRef.current.register(id, fn);
    }, []);
    const triggerScroll = useCallback((id, v) => {
        scrollSyncRef.current.scroll(id, v);
    }, []);
    return {
        tableState, modifyState,
        scrollVisible, modifyScrollVisible,
        scrollUtility: { register: scrollRefRegister, scroll: triggerScroll }
    };
}
export default useTableState;